package com.baizhi.service;

import com.baizhi.dao.OrdersMapper;
import com.baizhi.entity.Orders;
import com.baizhi.entity.OrdersExample;
import com.baizhi.entity.ProductOrder;
import com.baizhi.entity.UserOrders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service("orderService")
@Transactional
public class OrdersServiceImpl implements OrdersService {

    @Resource
    private OrdersMapper ordersMapper;

    @Autowired
    private ProductOrderService productOrderService;

    @Override
    public List<Orders> findAll() {
        OrdersExample ordersExample = new OrdersExample();
        return ordersMapper.selectByExample(ordersExample);
    }

    @Override
    public List<Orders> findByUserId(String userId) {
        OrdersExample ordersExample = new OrdersExample();
        ordersExample.createCriteria().andUseridEqualTo(userId);
        return ordersMapper.selectByExample(ordersExample);
    }

    @Override
    public List<UserOrders> showAll() {
        UserOrders userOrders = null;
        List<UserOrders> list = new ArrayList<>();
        List<Orders> all = ordersMapper.selectByExample(new OrdersExample());
        for (Orders orders : all) {
            List<ProductOrder> byOrderId = productOrderService.findByOrderId(orders.getId());
            for (ProductOrder productOrder : byOrderId) {
                userOrders = new UserOrders();
                userOrders.setOrderid(orders.getId());
                userOrders.setPreferential(orders.getPreferential());
                userOrders.setFreight(orders.getFreight());
                userOrders.setAddress(orders.getAddress());
                userOrders.setReceiver(orders.getReceiver());
                userOrders.setTel(orders.getTel());
                userOrders.setStatus(orders.getStatus());
                userOrders.setUserid(orders.getUserid());
                userOrders.setType(orders.getType());
                userOrders.setProductname(productOrder.getProductname());
                userOrders.setCount(productOrder.getCount());
                userOrders.setLease(productOrder.getLease());
                userOrders.setDeposit(productOrder.getDeposit());
                userOrders.setOrderdate(productOrder.getOverdate());
                userOrders.setOverdate(productOrder.getOverdate());
                userOrders.setStartdate(productOrder.getStartdate());
                userOrders.setPath(productOrder.getPath());
                userOrders.setProductid(productOrder.getProductid());
                userOrders.setPrice(productOrder.getPrice());
                userOrders.setProductid(productOrder.getProductid());
                userOrders.setProductorderid(productOrder.getId());
                list.add(userOrders);
            }
        }
        return list;
    }

    @Override
    public UserOrders findById(String orderid, String productorderid) {
        UserOrders userOrders = new UserOrders();
        Orders orders = ordersMapper.selectByPrimaryKey(orderid);
        ProductOrder productOrder = productOrderService.findById(productorderid);
        userOrders.setOrderid(orders.getId());
        userOrders.setPreferential(orders.getPreferential());
        userOrders.setFreight(orders.getFreight());
        userOrders.setAddress(orders.getAddress());
        userOrders.setReceiver(orders.getReceiver());
        userOrders.setTel(orders.getTel());
        userOrders.setStatus(orders.getStatus());
        userOrders.setUserid(orders.getUserid());
        userOrders.setType(orders.getType());
        userOrders.setCount(productOrder.getCount());
        userOrders.setLease(productOrder.getLease());
        userOrders.setDeposit(productOrder.getDeposit());
        userOrders.setOrderdate(productOrder.getOverdate());
        userOrders.setOverdate(productOrder.getOverdate());
        userOrders.setStartdate(productOrder.getStartdate());
        userOrders.setPath(productOrder.getPath());
        userOrders.setProductid(productOrder.getProductid());
        userOrders.setPrice(productOrder.getPrice());
        userOrders.setProductid(productOrder.getProductid());
        userOrders.setProductorderid(productOrder.getId());
        userOrders.setProductname(productOrder.getProductname());
        return userOrders;
    }

    @Override
    public void save(Orders orders) {
        orders.setId(UUID.randomUUID().toString());
        ordersMapper.insert(orders);
    }

    @Override
    public void update(Orders orders,String orderid) {
        orders.setId(orderid);
        ordersMapper.updateByPrimaryKeySelective(orders);
    }

    @Override
    public void delete(String id) {
        ordersMapper.deleteByPrimaryKey(id);
    }
}
